/*
 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores
 * CA 94065 USA or visit www.oracle.com if you need additional information or
 * have any questions.
 */

package com.codename1.designer;

import com.codename1.ui.util.EditableResources;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

/**
 * Adds a new image resource
 *
 * @author Shai Almog
 */
public class AddImageResource extends javax.swing.JDialog {
    private boolean okPressed;

    public AddImageResource(java.awt.Component c, EditableResources res) {
        this((java.awt.Frame)SwingUtilities.windowForComponent(c), true, res);
    }

    /** Creates new form AddImageResource */
    public AddImageResource(java.awt.Frame parent, boolean modal, EditableResources res) {
        super(parent, modal);
        initComponents();
        ModifiableJOptionPane.reverseOKCancel(ok, cancel);
        name.setText(AddResourceDialog.nextAvailableName(res, "Image"));
        pack();
        setLocationByPlatform(true);
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        name = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        type = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        help = new javax.swing.JTextPane();
        jLabel3 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        ok = new javax.swing.JButton();
        cancel = new javax.swing.JButton();

        FormListener formListener = new FormListener();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add Image");

        jLabel1.setText("Name");
        jLabel1.setName("jLabel1"); // NOI18N

        name.setName("name"); // NOI18N

        jLabel2.setText("Type");
        jLabel2.setName("jLabel2"); // NOI18N

        type.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "RGB Image", "Multi-Image", "Timeline", "GIF Animation" }));
        type.setName("type"); // NOI18N
        type.addActionListener(formListener);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        help.setContentType("text/html"); // NOI18N
        help.setEditable(false);
        help.setText("<html>\r\n  <head>\r\n\r\n  </head>\r\n  <body>\r\n    <p style=\"margin-top: 0\">\r\n      RGB Image is the default image used for the platform with the optimial number of colors on the given platform. When unsure this is probably what you are looking for.\r\n    </p>\r\n  </body>\r\n</html>\r\n"); // NOI18N
        help.setName("help"); // NOI18N
        jScrollPane1.setViewportView(help);

        jLabel3.setText("<html><body><b>To \"batch add\" multiple images use the images menu above</b>");
        jLabel3.setName("jLabel3"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new java.awt.GridLayout(1, 2));

        ok.setText("OK");
        ok.setName("ok"); // NOI18N
        ok.addActionListener(formListener);
        jPanel2.add(ok);

        cancel.setText("Cancel");
        cancel.setName("cancel"); // NOI18N
        cancel.addActionListener(formListener);
        jPanel2.add(cancel);

        jPanel1.add(jPanel2);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1)
                            .add(jLabel2))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(name)
                            .add(type, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel3))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(name, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(type, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == type) {
                AddImageResource.this.typeActionPerformed(evt);
            }
            else if (evt.getSource() == ok) {
                AddImageResource.this.okActionPerformed(evt);
            }
            else if (evt.getSource() == cancel) {
                AddImageResource.this.cancelActionPerformed(evt);
            }
        }
    }// </editor-fold>//GEN-END:initComponents

    private void typeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_typeActionPerformed
        String helpText = "RGB Image is the default image used for the platform with the optimial number of colors on the given platform. When unsure this is probably what you are looking for.";
        switch(type.getSelectedIndex()) {
            // Multi-Image
            case 1:
                helpText = "Multi images have a separate image instance per resolution range/DPI, this is useful for icons or complex elements that are resolution specific. Its not a good idea to use it for elements such as backgrounds etc. since device rotation, VKB and resolution ranges will break such images.";
                break;

            // SVG Image
            /*case 2:
                helpText = "SVG (Scalable Vector Graphics) files can be used on supported platforms, a set of fallback images can be defined which are stored as PNG's for the platforms that don't support SVG files.";
                break;*/

            // Timeline
            case 2:
                helpText = "Timeline allows defining simple animations mostly for splash screen/progress indication etc.";
                break;

            // GIF Animation
            case 3:
                helpText = "Animated GIF's can be imported and converted to a timeline, notice that multiple images will be created and this is a suboptimial way to create such effects.";
                break;
        }
        helpText = "<html><body><p style=\"margin-top: 0\">" + helpText + "</p></body></html>";
        help.setText(helpText);
    }//GEN-LAST:event_typeActionPerformed

    private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
        okPressed = true;
        dispose();
    }//GEN-LAST:event_okActionPerformed

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        dispose();
    }//GEN-LAST:event_cancelActionPerformed

    public boolean isOK() {
        return okPressed;
    }

    public String addResource(EditableResources res, ResourceEditorView view) {
        // "Image", "Animation", "Font", "Theme", "Data", "Localization (L10N)"
        String newName = name.getText();
        for(String r : res.getResourceNames()) {
            if(r.equalsIgnoreCase(newName)) {
                JOptionPane.showMessageDialog(this, "A resource called: " + newName + " already exists\nYou must delete the resource first.", "Add Resource", JOptionPane.ERROR_MESSAGE);
                return null;
            }
        }

        switch(type.getSelectedIndex()) {
            case 0: // RGB image
                ImageRGBEditor imageEditor = new ImageRGBEditor(res, name.getText(), view);
                imageEditor.selectFile();
                view.setSelectedResource(name.getText());
                break;
            case 1: // Multi image
                ImageMultiEditor multiImageEditor = new ImageMultiEditor(res, name.getText(), view);
                view.setSelectedResource(name.getText());
                break;
            case 2: // SVG
                MultiImageSVGEditor svgEditor = new MultiImageSVGEditor(res, name.getText());
                svgEditor.selectFile();
                view.setSelectedResource(name.getText());
                break;
            case 3: // timeline
                new TimelineEditor(res, name.getText(), view);
                view.setSelectedResource(name.getText());
                break;
            case 4: // Animated GIF
                TimelineEditor.selectFile(view, res, name.getText());
                break;
        }

        return name.getText();
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancel;
    private javax.swing.JTextPane help;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField name;
    private javax.swing.JButton ok;
    private javax.swing.JComboBox type;
    // End of variables declaration//GEN-END:variables

}
